function api()
{
	//properties
	this.player = 0;
	this.entities = {};
	this.myactions = [];
	this.otheractions = [];
	this.myships = [];
	this.mybases = [];
	this.otherships = [];
	this.otherbases = [];
	this.asteroids = [];
	this.futureactions = [];
	this.turn = 0;
	this.submitresponse = null;
	this.url = "http://coder9.selfip.com:8080/";
	
	//functions
	this.getturn = function getturn()
	{
		var eturn = 0;
		$.ajax({
		  url: this.url + "get_turn",
		  async: false,
		  dataType: 'json',
		  success: function (data) {
			eturn = data.turn;
		  }
		});
		
		this.turn = eturn;
		return this.turn;
	};
	
	this.clear = function clear()
	{
		console.log('clearing api');
		this.entities = {};
		this.myactions = [];
		this.otheractions = [];
		this.myships = [];
		this.mybases = [];
		this.otherships = [];
		this.otherbases = [];
		this.asteroids = [];
		this.futureactions = [];
	};
	
	this.submit = function submit()
	{
		console.log('submitting actions');
		var myJSONText = JSON.stringify(this.futureactions);
		$.post(this.url + "submit_actions",
				{actions:myJSONText},
				function(data){
					this.futureactions = [];
					this.submitresponse = data;
				},
				"json");
	};
	
	this.update = function update()
	{
		this.clear();
		console.log('updating api');
		var json_data = this.get_json_data();
		this.player = json_data.player_id;
		this.sortitems(json_data);
		this.turn = json_data.turn;
		this.myactions = json_data.actions;
	};
	
	this.get_json_data = function get_json_data()
	{
		var player_data = null;
		$.ajax({
		  url: this.url + "get_player_view_loggedin",
		  async: false,
		  dataType: 'json',
		  success: function (data) {
			player_data = data;
		  }
		});
		
		return player_data;
	};
	
	this.sortitems = function sortitems(items)
	{
		this.entities = items.view;
			
		for(var i in items.view)
		{
			item = items.view[i];
			if(item.type == 'ship')
			{
				if(item.player == this.player)
				{
					this.myships.push(item)
				}
				else
				{
					this.otherships.push(item)
				}
				
			}
			else if (item.type == 'base')
			{
				if(item.player == this.player)
				{
					this.mybases.push(item)
				}
				else
				{
					this.otherbases.push(item)
				}
			}
			else if (item.type == 'asteroid')
			{
				this.asteroids.push(item)
			}
		}
	};
	
	this.add_attack = function add_attack(attacker,target)
	{
		var a = {'type':'attack','atk':attacker,'tgt':target};
		this.futureactions.push(a);
	};
	
	this.add_mine = function add_mine(miner,asteroid,base)
	{
		var a = {'type':'mine','miner':miner,'asteroid':asteroid,'base':base};
		this.futureactions.push(a);
	};
	
	this.add_move = function add_move(x,y,idx)
	{
		var a = {'type':'move','idx':idx,'x':x,'y':y};
		this.futureactions.push(a);
	};
	
	this.add_create = function add_create(x,y,what,base)
	{
		var a = {'type':'create','what':what,'base':base,'x':x,'y':y,'player':this.player};
		this.futureactions.push(a);
	};
}

function object_2_string(o)
{
	var str = '';
	for(var i in o)
	{
		str += i + ':' + o[i].toString() + ' ';
	}
	return str;
}





















